package ro_tool

import (
	"back/service/tool"
	"github.com/gin-gonic/gin"
	"utilx/rox"
)

type DashboardRouter struct {
	srv tool.DashboardService
}

func NewDashboardRouter(srv tool.DashboardService) DashboardRouter {
	return DashboardRouter{srv}
}
func (b DashboardRouter) InitRouter(Router *gin.RouterGroup) {
	cmdRouter := Router.Group("dashboard")
	//Use(middleware.JwtAuth()).
	//Use(middleware.SessionAuth()).
	//Use(middleware.PasswordExpired())
	{
		cmdRouter.GET("/base/os", b.LoadDashboardOsInfo)
		cmdRouter.GET("/base/:ioOption/:netOption", b.LoadDashboardBaseInfo)
		cmdRouter.GET("/current/:ioOption/:netOption", b.LoadDashboardCurrentInfo)
		cmdRouter.POST("/system/restart/:operation", b.SystemRestart)
	}
}

// @Tags Dashboard
// @Summary Load os info
// @Description 获取服务器基础数据
// @Accept json
// @Success 200 {object} dto.OsInfo
// @Security ApiKeyAuth
// @Router /dashboard/base/os [get]
func (b DashboardRouter) LoadDashboardOsInfo(c *gin.Context) {
	data, err := b.srv.LoadOsInfo()
	if err != nil {
		rox.HandleErr[string](c, "login err", err.Error())
		return
	}
	rox.HandleSuccess(c, "success", data)
}

// @Tags Dashboard
// @Summary Load dashboard base info
// @Description 获取首页基础数据
// @Accept json
// @Param ioOption path string true "request"
// @Param netOption path string true "request"
// @Success 200 {object} dto.DashboardBase
// @Security ApiKeyAuth
// @Router /dashboard/base/:ioOption/:netOption [get]
func (b DashboardRouter) LoadDashboardBaseInfo(c *gin.Context) {
	ioOption, ok := c.Params.Get("ioOption")
	if !ok {
		rox.HandleErr[any](c, "error ioOption in path", nil)
		return
	}
	netOption, ok := c.Params.Get("netOption")
	if !ok {
		rox.HandleErr[any](c, "error netOption in path", nil)

		return
	}
	data, err := b.srv.LoadBaseInfo(ioOption, netOption)
	if err != nil {
		rox.HandleErr[string](c, "login err", err.Error())
		rox.HandleErr[any](c, err.Error(), nil)

		return
	}
	rox.HandleSuccess(c, "success", data)

}

// @Tags Dashboard
// @Summary Load dashboard current info
// @Description 获取首页实时数据
// @Accept json
// @Param ioOption path string true "request"
// @Param netOption path string true "request"
// @Success 200 {object} dto.DashboardCurrent
// @Security ApiKeyAuth
// @Router /dashboard/current/:ioOption/:netOption [get]
func (b DashboardRouter) LoadDashboardCurrentInfo(c *gin.Context) {
	ioOption, ok := c.Params.Get("ioOption")
	if !ok {
		rox.HandleErr[any](c, "error ioOption in path", nil)

		return
	}
	netOption, ok := c.Params.Get("netOption")
	if !ok {
		rox.HandleErr[any](c, "error netOption in path", nil)
		return
	}

	data := b.srv.LoadCurrentInfo(ioOption, netOption)
	rox.HandleSuccess(c, "success", data)

}

// @Tags Dashboard
// @Summary System restart
// @Description 重启服务器/面板
// @Accept json
// @Param operation path string true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /dashboard/system/restart/:operation [post]
func (b DashboardRouter) SystemRestart(c *gin.Context) {
	operation, ok := c.Params.Get("operation")
	if !ok {
		rox.HandleErr[any](c, "error operation in path", nil)
		return
	}
	if err := b.srv.Restart(operation); err != nil {
		rox.HandleErr[any](c, err.Error(), nil)
		return
	}
	rox.HandleSuccess[any](c, "success", nil)

}
